package com.syqc.monitor.popup

import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.annotation.StyleRes
import com.syqc.comlib.custom.timer.DateFormatUtils
import com.syqc.comlib.custom.timer.DatePicker
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.DialogVideoTimeBinding
import com.syqc.utils.TimeUtil

class VideoTimeDialog(
    context: Context,
    @StyleRes themeResId: Int = R.style.CustomDialog,
    private val onDownLoad: (String, String, String, String, String, Boolean) -> Unit
) : AlertDialog(context, themeResId) {
    private var startTime = ""
    private var endTime = ""
    private var isStart = true
    private var isChange = false

    //视频格式
    private var videoType = ""
    private lateinit var binding: DialogVideoTimeBinding

    private val mTimePicker by lazy {
        DatePicker(context, callback).apply { setCanShowPreciseTime() }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogVideoTimeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setCancelable(false)
        setCanceledOnTouchOutside(false)


        binding.tvPlatCancel.setOnClickListener {
            dismiss()
        }

        binding.tvPlatSure.setOnClickListener {
            val stime = TimeUtil.instance.formatHm(startTime)
            val etime = TimeUtil.instance.formatHm(endTime)
            val isNotOk = TimeUtil.instance.reduceHms(stime, etime)
            if (isNotOk) {
                R.string.toast_start_less_than_end_time.toast(context)
                return@setOnClickListener
            }
            onDownLoad.invoke(stime, etime, startTime, endTime, videoType, isChange)
            dismiss()
        }

        binding.tvTimeStart.setOnClickListener {
            isStart = true
            mTimePicker.show(startTime)
        }

        binding.tvTimeEnd.setOnClickListener {
            isStart = false
            mTimePicker.show(endTime)
        }

        binding.videoSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?, view: View?, position: Int, id: Long
            ) {
                when (position) {
                    0 -> videoType = ""
                    1 -> videoType = "mp4"
                    2 -> videoType = "flv"
                    3 -> videoType = "mpegts"
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
    }

    fun setTime(start: String, end: String) {
        startTime = changeTime(start)
        endTime = changeTime(end)
        isChange = false
        binding.tvTimeStart.text = startTime
        binding.tvTimeEnd.text = endTime
        mTimePicker.setStartEnd(startTime, endTime)
    }

    private fun changeTime(time: String): String {
        val yyyy = time.substring(0, 4)
        val MM = time.substring(4, 6)
        val dd = time.substring(6, 8)
        val hh = time.substring(8, 10)
        val mm = time.substring(10, 12)
        val ss = time.substring(12, 14)
        return "$yyyy-$MM-$dd $hh:$mm:$ss"
    }

    private val callback: DatePicker.Callback =
        object : DatePicker.Callback {
            override fun onTimeSelected(timestamp: Long) {
                val time: String = DateFormatUtils.long2Second(timestamp)
                if (isStart) {
                    startTime = time
                    binding.tvTimeStart.text = time
                } else {
                    endTime = time
                    binding.tvTimeEnd.text = time
                }
                isChange = true
                mTimePicker.dismiss()
            }
        }
}